class Solution {
public:
    int minMaxDifference(int num) {
        
        int maxn=num,minn=num;
        for(char i='0';i<'9';i++){

            int sum=0;
            string tmp=to_string(num);
            for(int j=0;j<tmp.size();j++){

                if(tmp[j]==i)
                {
                    tmp[j]='9';
                }
                sum=sum*10+(tmp[j]-'0');
            }
            maxn=max(maxn,sum);
        }
        for(char i='1';i<='9';i++){

            int sum=0;
            string tmp=to_string(num);
            for(int j=0;j<tmp.size();j++){

                if(tmp[j]==i)
                {
                    tmp[j]='0';
                }
                sum=sum*10+(tmp[j]-'0');
            }
            minn=min(minn,sum);
        }      
        return maxn-minn;
    }
};
